# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=rust-bindgen
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.71.1
pkgrel=1
pkgdesc='Automatically generates Rust FFI bindings to C (and some C++) libraries (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://rust-lang.github.io/rust-bindgen/'
msys2_repository_url='https://github.com/rust-lang/rust-bindgen'
msys2_references=(
  'archlinux: rust-bindgen'
  'purl: pkg:cargo/bindgen'
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-clang" "${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-mdbook")
source=("${msys2_repository_url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('620d80c32b6aaf42d12d85de86fc56950c86b2a13a5b943c10c29d30c4f3efb0')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"

  "${MINGW_PREFIX}/bin/cargo.exe" build \
    --release \
    --frozen

  cd book
  "${MINGW_PREFIX}/bin/mdbook.exe" build
}

check() {
  cd "${srcdir}/${_realname}-${pkgver}"

  "${MINGW_PREFIX}/bin/cargo.exe" test \
    --release \
    --locked
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"

  install -Dm755 "target/release/bindgen.exe" "${pkgdir}${MINGW_PREFIX}/bin/bindgen.exe"
  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  cp -a "book/book" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html"
  install -Dm644 "LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
